<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :span="24" :xs="24">
        <!-- 数据筛选表单 -->
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="100px">
          <!-- 数据筛选表单组件 -->
          <el-form-item label="订单编号" prop="orderNo">
            <el-input v-model="queryParams.orderNo" placehold clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="客户姓名" prop="customerName">
            <el-input v-model="queryParams.customerName" placehold clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="客户电话" prop="customerPhone">
            <el-input v-model="queryParams.customerPhone" placehold clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="预约开始时间" prop="orderTime">
            <el-input v-model="queryParams.orderTime" placehold clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="会员ID" prop="vipId">
            <el-input v-model="queryParams.vipId" placehold clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="发型师姓名" prop="tonyName">
            <el-input v-model="queryParams.tonyName" placehold clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="技师姓名" prop="assName">
            <el-input v-model="queryParams.assName" placehold clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="总次数（各次数相加）" prop="totalQuantity">
            <el-input v-model="queryParams.totalQuantity" placehold clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="总金额（各小计金额相加）" prop="totalPrice">
            <el-input v-model="queryParams.totalPrice" placehold clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="订单状态（0：已预约，1：挂账中）" prop="status">
            <el-input v-model="queryParams.status" placehold clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="逻辑删除（0：隐藏，1：显示）" prop="display">
            <el-input v-model="queryParams.display" placehold clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="创建人" prop="createBy">
            <el-input v-model="queryParams.createBy" placehold clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
            <el-input v-model="queryParams.createTime" placehold clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="更新人" prop="updateBy">
            <el-input v-model="queryParams.updateBy" placehold clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="更新时间" prop="updateTime">
            <el-input v-model="queryParams.updateTime" placehold clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <!-- 数据筛选操作按钮 -->
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="3">
            <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">新增
            </el-button>
          </el-col>
        </el-row>
        
        <!-- 数据表格 -->
        <el-table v-loading="loading" :data="orderList">
          <!-- 数据表格列 -->
          <el-table-column label="订单id" align="center" prop="orderId" />
          <el-table-column label="订单编号" align="center" prop="orderNo" />
          <el-table-column label="客户姓名" align="center" prop="customerName" />
          <el-table-column label="客户电话" align="center" prop="customerPhone" />
          <el-table-column label="预约开始时间" align="center" prop="orderTime" />
          <el-table-column label="会员ID" align="center" prop="vipId" />
          <el-table-column label="发型师姓名" align="center" prop="tonyName" />
          <el-table-column label="技师姓名" align="center" prop="assName" />
          <el-table-column label="总次数（各次数相加）" align="center" prop="totalQuantity" />
          <el-table-column label="总金额（各小计金额相加）" align="center" prop="totalPrice" />
          <el-table-column label="订单状态（0：已预约，1：挂账中）" align="center" prop="status" />
          <el-table-column label="逻辑删除（0：隐藏，1：显示）" align="center" prop="display" />
          <el-table-column label="创建人" align="center" prop="createBy" />
          <el-table-column label="创建时间" align="center" prop="createTime" />
          <el-table-column label="更新人" align="center" prop="updateBy" />
          <el-table-column label="更新时间" align="center" prop="updateTime" />
          <!-- 数据行操作按钮 -->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="small" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button size="small" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 数据分页加载 -->
        <el-pagination
          :current-page="pageNum"
          :page-sizes="[5, 10, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加修改order对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!-- 数据编辑表单组件 -->
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="客户姓名" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户姓名" />
        </el-form-item>
        <el-form-item label="客户电话" prop="customerPhone">
          <el-input v-model="form.customerPhone" placeholder="请输入客户电话" />
        </el-form-item>
        <el-form-item label="预约开始时间" prop="orderTime">
          <el-input v-model="form.orderTime" placeholder="请输入预约开始时间" />
        </el-form-item>
        <el-form-item label="会员ID" prop="vipId">
          <el-input v-model="form.vipId" placeholder="请输入会员ID" />
        </el-form-item>
        <el-form-item label="发型师姓名" prop="tonyName">
          <el-input v-model="form.tonyName" placeholder="请输入发型师姓名" />
        </el-form-item>
        <el-form-item label="技师姓名" prop="assName">
          <el-input v-model="form.assName" placeholder="请输入技师姓名" />
        </el-form-item>
        <el-form-item label="总次数（各次数相加）" prop="totalQuantity">
          <el-input v-model="form.totalQuantity" placeholder="请输入总次数（各次数相加）" />
        </el-form-item>
        <el-form-item label="总金额（各小计金额相加）" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入总金额（各小计金额相加）" />
        </el-form-item>
        <el-form-item label="订单状态（0：已预约，1：挂账中）" prop="status">
          <el-input v-model="form.status" placeholder="请输入订单状态（0：已预约，1：挂账中）" />
        </el-form-item>
        <el-form-item label="逻辑删除（0：隐藏，1：显示）" prop="display">
          <el-input v-model="form.display" placeholder="请输入逻辑删除（0：隐藏，1：显示）" />
        </el-form-item>
        <el-form-item label="创建人" prop="createBy">
          <el-input v-model="form.createBy" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-input v-model="form.createTime" placeholder="请输入创建时间" />
        </el-form-item>
        <el-form-item label="更新人" prop="updateBy">
          <el-input v-model="form.updateBy" placeholder="请输入更新人" />
        </el-form-item>
        <el-form-item label="更新时间" prop="updateTime">
          <el-input v-model="form.updateTime" placeholder="请输入更新时间" />
        </el-form-item>
      </el-form>
      <!-- 数据提交操作按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
/** 导入axios Api */
import { listOrderByPage, getOrder, addOrder, updateOrder, deleteOrder } from '@/api/order'
export default {
  name: 'Order',
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 当前页
      pageNum: 0,
      // 每页数据量
      pageSize: 0,
      // Order表格数据
      orderList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 数据查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        orderId: null,
        orderNo: null,
        customerName: null,
        customerPhone: null,
        orderTime: null,
        vipId: null,
        tonyName: null,
        assName: null,
        totalQuantity: null,
        totalPrice: null,
        status: null,
        display: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      },
      // 数据编辑表单
      form: {},
      // 表单数据校验规则
      rules: {
        orderId: [
          { required: true, message: '订单id不能为空', trigger: 'blur' }
        ],
        orderNo: [
          { required: true, message: '订单编号不能为空', trigger: 'blur' }
        ],
        customerName: [
          { required: true, message: '客户姓名不能为空', trigger: 'blur' }
        ],
        customerPhone: [
          { required: true, message: '客户电话不能为空', trigger: 'blur' }
        ],
        orderTime: [
          { required: true, message: '预约开始时间不能为空', trigger: 'blur' }
        ],
        vipId: [
          { required: true, message: '会员ID不能为空', trigger: 'blur' }
        ],
        tonyName: [
          { required: true, message: '发型师姓名不能为空', trigger: 'blur' }
        ],
        assName: [
          { required: true, message: '技师姓名不能为空', trigger: 'blur' }
        ],
        totalQuantity: [
          { required: true, message: '总次数（各次数相加）不能为空', trigger: 'blur' }
        ],
        totalPrice: [
          { required: true, message: '总金额（各小计金额相加）不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '订单状态（0：已预约，1：挂账中）不能为空', trigger: 'blur' }
        ],
        display: [
          { required: true, message: '逻辑删除（0：隐藏，1：显示）不能为空', trigger: 'blur' }
        ],
        createBy: [
          { required: true, message: '创建人不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ],
        updateBy: [
          { required: true, message: '更新人不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: '更新时间不能为空', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询Order数据列表 */
    getList() {
      this.loading = true
      listOrderByPage(this.queryParams).then((response) => {
        this.orderList = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 处理分页 */
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getList()
    },

    /** 数据编辑对话框取消按钮 */
    cancel() {
      this.open = false
      this.resetForm()
    },

    /** 数据编辑表单清空 */
    resetForm() {
      this.form = {
        orderId: null,
        orderNo: null,
        customerName: null,
        customerPhone: null,
        orderTime: null,
        vipId: null,
        tonyName: null,
        assName: null,
        totalQuantity: null,
        totalPrice: null,
        status: null,
        display: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      }
    },
    
    /** 重置分页 */
    resetQueryParams() {
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 5
    },

    /** 数据筛选搜索按钮 */
    handleQuery() {
      this.resetQueryParams()
      this.getList()
      this.$message.success('查询成功')
    },

    /** 数据筛选重置按钮 */
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.getList()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm()
      this.open = true
      this.title = '添加Order'
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.resetForm()
      getOrder(row.orderId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改Order'
      })
    },

    /** 数据提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.orderId !== null) {
            updateOrder(this.form).then((response) => {
              this.open = false
              this.title = ''
              this.getList()
              this.$message.success(response.data)
            })
          } else {
            addOrder(this.form).then((response) => {
              this.open = false
              this.title = ''
              this.getList()
              this.$message.success(response.data)
            })
          }
        }
      })
    },
    
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除ID为"' + row.orderId + '"的数据项?', '警告', {
          confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
        })
        .then(function() {
          return deleteOrder(row.orderId)
        })
        .then(() => {
          this.getList()
          this.$message.success('删除成功')
        })
        .catch(() => {})
    }
  }
}
</script>
